package com.example.jiugongge;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class LockPatt extends View {
    private boolean hasInit = false;
    float width;
    float height;
    float offsetY;
    float offsetX;
    private Bitmap bitmap;
    private Bitmap bitmap2;
    private Bitmap bitmap3;
    private Point[][] points;
    private float r;
    private float evenX;
    private float evenY;
    private boolean isSelected;
    private boolean moveOnPoint = true;
    private boolean isFinish;
    Point chenckPoin;
    Paint paint = new Paint();
    private ArrayList<Point> pointArrayList = new ArrayList<Point>();

    public LockPatt(Context context) {
        super(context);
    }

    public LockPatt(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LockPatt(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        没有结束
        isFinish = false;
//        默认用户会滑动到点上
        moveOnPoint = true;

        evenX = event.getX();
        evenY = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                rest();
//                按下 判断用户是否按在了九宫格上
                chenckPoin = chenckPoin(evenX, evenY, r);
                if(chenckPoin!=null){
                    isSelected = true;
                    chenckPoin.setState(Point.STATE_PRESSED);
                }

                break;
            case MotionEvent.ACTION_MOVE:
                // 手指移动的时候
                if(isSelected){
                    chenckPoin = chenckPoin(evenX, evenY, r);
                    if(chenckPoin!=null){
                        chenckPoin.setState(Point.STATE_PRESSED);
                        moveOnPoint = true;

                    }else{
                        moveOnPoint = false;

                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                isFinish = true;
                isSelected = false;

                break;
            case MotionEvent.ACTION_CANCEL:
                isFinish = true;
                isSelected = false;
                break;

        }

        if (isSelected&&!isFinish&&chenckPoin!=null){
            pointArrayList.add(chenckPoin);
        }
        // 刷新onDraw方法
        postInvalidate();

        // 消费此触摸事件
        return true;
    }

    /**
     * 重置九宫格所有的点
     */
    private void rest() {
//        将所有集合中的点修改为正常对象
        for (int i = 0; i < pointArrayList.size(); i++) {
            pointArrayList.get(i).setState(Point.getStateNormal());
        }
//        将集合清空
        pointArrayList.clear();
    }

    /**
     * 计算按下或移动的位置是否在九宫格上
     *  @param evenX
     * @param evenY
     * @param r
     * @return
     */
    private Point chenckPoin(float evenX, float evenY, float r) {
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < points[i].length; j++) {
                Point point = points[i][j];
                // 判断
                double distance = getDistance(point.x, point.y, evenX, evenY);
                if(distance<r){
                    return point;
                }
            }
        }
        return null;
    }

    private double getDistance(float x1, float y1, float x2, float y2) {
        return Math.sqrt(Math.abs(x1-x2)*Math.abs(x1-x2)+Math.abs(y1-y2)*Math.abs(y1-y2));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(!hasInit){
            initPoints();
//            初始化画笔
            initPaint();
            hasInit = true;
        }
        drawPoints(canvas);
//        画线
        drawLines(canvas);

    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        paint.setColor(Color.GREEN);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(12);
    }

    private void drawLines(Canvas canvas) {
        if(pointArrayList.size() > 0){
            Point a = pointArrayList.get(0);
            for (int i = 1; i < pointArrayList.size(); i++) {
                // 获取第b个点
                Point b = pointArrayList.get(i);
//                画线
                canvas.drawLine(a.x, a.y, b.x, b.y, paint);
                //将b点赋值给a
                a = b;
            }
            if(!moveOnPoint){
                // 单纯的画线
                canvas.drawLine(a.x, a.y, evenX, evenY, paint);
            }
        }
    }

    private void drawPoints(Canvas canvas) {
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < points[i].length; j++) {
                Point point = points[i][j];
                if(point.getState() == Point.STATE_NORMAL){
                    // 获取到当前点的坐标, 减去点的图片的半径， 得到left， top的坐标点
                    canvas.drawBitmap(bitmap, point.x-r, point.y - r, null);
                }
                if(point.getState() == Point.STATE_PRESSED){
                    canvas.drawBitmap(bitmap2, point.x-r, point.y - r, null);

                }
                if(point.getState() == Point.STATE_ERROR){
                    canvas.drawBitmap(bitmap3, point.x-r, point.y - r, null);
                }
            }
        }
    }

    private void initPoints() {
        width = getWidth();
        height = getHeight();
        offsetY = 0;
        offsetX = 0;
        // 竖屏
        if(height > width){
            offsetY = (height - width) / 2;
            height = width;
        }else{
//            横屏幕
            offsetX = (width - height) / 2;
            width = height;
        }
        /**
         * 定义九个点的坐标
         */
        points = new Point[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                points[i][j] = new Point(offsetX + (width/4) * (i + 1), offsetY + (width / 4) * (j + 1));
            }

        }
        /**
         * 实例化控件图片
         */
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.jiu1);
        bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.jiu2);
        bitmap3 = BitmapFactory.decodeResource(getResources(), R.drawable.jiu3);

        r = bitmap.getWidth()/2;


    }
}
